//
//  MyPublishViewController.m
//  cityStar
//
//  Created by Hancong on 15/3/3.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "MyPublishViewController.h"
#import "VideoListCell.h"
#import "VideoModel.h"
#import "VideoViewDetailViewController.h"
#import "MakefirendsListCell.h"
#import "MakeFriendModel.h"
#import "MakefirendsDetailViewController.h"
#import "OtherModel.h"
#import "OtherCell.h"
#import "RecruitmentModel.h"
#import "RecruitmentListCell.h"
#import "RecruitmentDetailViewController.h"
#import "CarpoolingModel.h"
#import "CarpoolingListCell.h"
#import "CarpoolingDetailViewController.h"
#import "HomeModel.h"
#import "HomeEconomicsListCell.h"
#import "HomeEconomicDetailViewController.h"
#import "LawListCell.h"
#import "LawDetailViewCotroller.h"
#import "LawArrayModel.h"
#import "SVPullToRefresh.h"

NSString *const VideoPublishListCellID = @"videoPublishListCellID";
NSString *const MakefirendsPublishListCellID = @"makefirendsPublishListCellID";
NSString *const OtherPublishCellID = @"otherPublishCellID";
NSString *const RecruitmentPublishListCellID = @"recruitmentPublishListCellID";
NSString *const carpoolingPublishListCellID = @"CarpoolingPublishListCellID";
NSString *const HomeEconomicsPublishListCellID = @"HomeEconomicsPublishListCellID";
NSString *const LawListPublishCellID = @"lawListPublishCellID";

@interface MyPublishViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageLineFrame;
@property (strong, nonatomic) NSMutableArray *requestArray;
@property (assign, nonatomic) int page;
@property (nonatomic, weak) IBOutlet UILabel *nothinglabel;

@end

@implementation MyPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.kPublishStatues = KOrderStatuesVideo;
    [self addBackButton];
    [self setUpViews];
    [self setUpTableViewCells];
    self.kPublishStatues = KOrderStatuesVideo;
//    [self webRequest_GetMyPulishWithButtonType:2 isMore:NO];
    self.btnVideo.selected = YES;
    __weak typeof(self)weakSelf = self;
    [self.tableView addPullToRefreshWithActionHandler:^{
        self.page = 0;
        [weakSelf webRequest_GetMyPulishWithButtonType:weakSelf.kPublishStatues isMore:NO];
    }];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetMyPulishWithButtonType:weakSelf.kPublishStatues isMore:YES];
    }];
    [self.tableView triggerPullToRefresh];
}

-(void)webRequest_GetMyPulishWithButtonType:(int)type isMore:(BOOL)isMore
{
    if (!isMore) {
        self.requestArray = [[NSMutableArray alloc]init];
    }else{
        self.page++;
    }
    NSString *postUrls = [NSString stringWithFormat:@"%@/type/%i/page/%i/mine/%i",KInterface_PublishList,type,self.page,1];
    [HttpRequestManager getMethod:postUrls parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [self.requestArray addObjectsFromArray:response[@"value"][0][@"content"]];
            [self.tableView reloadData];

            if (self.requestArray.count == 0) {
                self.nothinglabel.hidden = NO;
//                [SVProgressHUD showErrorWithStatus:@"暂无内容，快去发布吧"];
            }else{
                self.nothinglabel.hidden = YES;
            }
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
    if (isMore){
        [self.tableView.infiniteScrollingView stopAnimating];
    }else{
        [self.tableView.pullToRefreshView stopAnimating];
    }
}

#pragma mark - setUps
-(void)setUpViews
{
    self.title = @"我的发布";
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(AppBounds)+200, 45);
}

-(void)setUpTableViewCells
{
    [self.tableView registerNib:[VideoListCell nib] forCellReuseIdentifier:VideoPublishListCellID];
    [self.tableView registerNib:[MakefirendsListCell nib] forCellReuseIdentifier:MakefirendsPublishListCellID];
    [self.tableView registerNib:[OtherCell nib] forCellReuseIdentifier:OtherPublishCellID];
    [self.tableView registerNib:[RecruitmentListCell nib] forCellReuseIdentifier:RecruitmentPublishListCellID];
    [self.tableView registerNib:[CarpoolingListCell nib] forCellReuseIdentifier:carpoolingPublishListCellID];
    [self.tableView registerNib:[HomeEconomicsListCell nib] forCellReuseIdentifier:HomeEconomicsPublishListCellID];
    [self.tableView registerNib:[LawListCell nib] forCellReuseIdentifier:LawListPublishCellID];

}

- (IBAction)btnSelect_Touch:(UIButton *)sender {
    float x = sender.frame.origin.x;
    [UIView animateWithDuration:0.2 animations:^{
        self.imageLineFrame.constant = x;
        [self.view layoutIfNeeded];
    }];
    [self refreshButtons];
    sender.selected = YES;
    self.kPublishStatues = sender.tag;
    [self webRequest_GetMyPulishWithButtonType:(int)sender.tag isMore:NO];
}

-(void)refreshButtons
{
    self.page = 0;
    self.btnLaw.selected = NO;
    self.btnVideo.selected = NO;
    self.btnFriend.selected = NO;
    self.btnOthers.selected = NO;
    self.btnActives.selected = NO;
    self.btnCarpool.selected = NO;
    self.btnRecruit.selected = NO;
    self.btnHomemaking.selected = NO;
}

#pragma mark - setUps
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.kPublishStatues == KOrderStatuesFriend) {
        return [MakefirendsListCell heightOfTableCell];
    }else if (self.kPublishStatues == KOrderStatuesOthers){
        return [OtherCell heightOfTableCell];
    }else if (self.kPublishStatues == KOrderStatuesRecruit){
        return [RecruitmentListCell heightOfTableCell];
    }else if (self.kPublishStatues == KOrderStatuesCarpool){
        return [CarpoolingListCell heightOfTableCell];
    }else if (self.kPublishStatues == KOrderStatuesHomemaking){
        return [HomeEconomicsListCell heightOfTableCell];
    }else if (self.kPublishStatues == KOrderStatuesLaw){
        return [LawListCell heightOfTableCell];
    }
    else{
        return [VideoListCell heightOfTableCell];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.requestArray.count;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.kPublishStatues == KOrderStatuesFriend) {
        MakefirendsListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:MakefirendsPublishListCellID];
        MakeFriendArrayModel *model = [MakeFriendArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        [ListdetailCell setDataMakefirend:model];
        ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ListdetailCell;
    }else if (self.kPublishStatues == KOrderStatuesOthers) {
        OtherCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:OtherPublishCellID];
        ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ListdetailCell;
    }else if (self.kPublishStatues == KOrderStatuesRecruit){
        RecruitmentListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:RecruitmentPublishListCellID];
        RecruitmetArrayModel *model = [RecruitmetArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        [ListdetailCell setdataRecruit:model];
        ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ListdetailCell;
    }else if (self.kPublishStatues == KOrderStatuesCarpool){
        CarpoolingListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:carpoolingPublishListCellID];
        CarpoolingListModel *carList = [CarpoolingListModel objectFromDictionary:self.requestArray[indexPath.row]];
        ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        [ListdetailCell setCityPollingData:carList];
        return ListdetailCell;
    }else if (self.kPublishStatues == KOrderStatuesHomemaking){
        HomeEconomicsListCell *homeEconomicsListCell = [tableView dequeueReusableCellWithIdentifier:HomeEconomicsPublishListCellID];
        HomeArrayModel *model = [HomeArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        [homeEconomicsListCell setDataHomeModel:model];
        homeEconomicsListCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return homeEconomicsListCell;
    }else if (self.kPublishStatues == KOrderStatuesLaw) {
        LawListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:LawListPublishCellID];
        LawArrayModel *model = [LawArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        [ListdetailCell setDataLaw:model];
        ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ListdetailCell;
    }
    else{
    VideoListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:VideoPublishListCellID];
    VideoArrayModel *model = [VideoArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
    [ListdetailCell setDataVieo:model];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
        return ListdetailCell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.kPublishStatues == KOrderStatuesFriend) {
        MakeFriendArrayModel *model = [MakeFriendArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        MakefirendsDetailViewController *makefirendsDetailViewController = [[MakefirendsDetailViewController alloc]init];
        makefirendsDetailViewController.freienModel = model;
        [self.navigationController pushViewController:makefirendsDetailViewController animated:YES];
        [tableView reloadData];
    }else if (self.kPublishStatues == KOrderStatuesOthers){
        
    }else if (self.kPublishStatues == KOrderStatuesRecruit) {
        RecruitmetArrayModel *model = [RecruitmetArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        RecruitmentDetailViewController *recruitmentDetailViewController = [[RecruitmentDetailViewController alloc]init];
        recruitmentDetailViewController.recruitmentModel = model;
        [self.navigationController pushViewController:recruitmentDetailViewController animated:YES];
        [tableView reloadData];
    }else if (self.kPublishStatues == KOrderStatuesCarpool){
        [tableView reloadData];
        CarpoolingListModel *carList = [CarpoolingListModel objectFromDictionary:self.requestArray[indexPath.row]];
        CarpoolingDetailViewController *carpoolingViewController = [[CarpoolingDetailViewController alloc]init];
        carpoolingViewController.carModel = carList;
        [self.navigationController pushViewController:carpoolingViewController animated:YES];
    }else if (self.kPublishStatues == KOrderStatuesHomemaking) {
        HomeEconomicDetailViewController *homeEconomicdetailViewController = [[HomeEconomicDetailViewController alloc]init];
        HomeArrayModel *model = [HomeArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        homeEconomicdetailViewController.homeModel = model;
        homeEconomicdetailViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:homeEconomicdetailViewController animated:YES];
        [tableView reloadData];
    }else if (self.kPublishStatues == KOrderStatuesLaw) {
        LawArrayModel *model = [LawArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
        LawDetailViewCotroller *lawdetailViewController = [[LawDetailViewCotroller alloc]init];
        lawdetailViewController.hidesBottomBarWhenPushed = YES;
        lawdetailViewController.lawModel = model;
        [self.navigationController pushViewController:lawdetailViewController animated:YES];
        [tableView reloadData];
    }
    else{
    VideoArrayModel *model = [VideoArrayModel objectFromDictionary:self.requestArray[indexPath.row]];
    VideoViewDetailViewController *videoViewController = [[VideoViewDetailViewController alloc]init];
    videoViewController.videoModel = model;
    [self.navigationController pushViewController:videoViewController animated:YES];
    [tableView reloadData];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
